package net.dgg.zz.credential.baseCredential.service;

import net.dgg.iboss.base.service.CmsService;
import net.dgg.zz.common.constant.CredentialConstant;
import net.dgg.zz.common.utils.StringUtils;
import net.dgg.zz.common.utils.ValidateUtils;
import net.dgg.zz.credential.baseCredential.dao.BaseCredentialDao;
import net.dgg.zz.credential.baseCredential.entity.BaseCredential;
import net.dgg.zz.credential.excption.CertificateExcption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 证书基础信息
 *
 * @author wmj
 * @date 2018/8/13 10:54
 */
@Service
public class BaseCredentialService {

    @Autowired
    private BaseCredentialDao baseCredentialDao;
    @Autowired
    private CmsService cmsService;

    public int insert(BaseCredential record) {
        return baseCredentialDao.insert(record);
    }

    public int updateByPrimaryKeySelective(BaseCredential record) {
        return baseCredentialDao.updateByPrimaryKeySelective(record);
    }

    public BaseCredential selectByPrimaryKey(Long id) {
        return baseCredentialDao.selectByPrimaryKey(id);
    }

    /**
     * 根据证书编号验重
     *
     * @param params 参数可包含 证书编号 证书(资质)类型  证书id
     * @return
     */
    public BaseCredential checkCredentialNo(Map params) {
        BaseCredential baseCredential = baseCredentialDao.findCredentialByNo(params);
        if (StringUtils.isNotEmpty(baseCredential)) {
            // 编辑证书时判断查询出的证书id是否相等
            if (params.containsKey("certificateId") && Long.parseLong(params.get("certificateId").toString()) == baseCredential.getId().longValue()) {
                return null;
            }
            if (params.containsKey("id") && Long.parseLong(params.get("id").toString()) == baseCredential.getId().longValue()) {
                return null;
            }
            // 如果是普通证书的编号重复
            if (params.containsKey("certificateType") && StringUtils.isNotEmpty(params.get("certificateType"))) {
                // 证书状态不为退回
                ValidateUtils.isTrue(baseCredential.getCertificateStatus().equals(CredentialConstant.GENERAL_CREDENTIAL_STATUS_RETURN), CertificateExcption.class, "证书编号重复");
                return baseCredential;
            }
            ValidateUtils.isTrue(params.containsKey("certificateId") && Long.parseLong(params.get("certificateId").toString()) == baseCredential.getId().longValue(), CertificateExcption.class, "证书编号重复");

        }
        return null;
    }

    /*
     * @Author zsx
     * @Date 2018-09-14 07:09:50
     * @Description 更具资质编号验充
     * @param null
     * @return null
     */

    public void checkAptitudeNo(Map params) {
        params.put("generalType", CredentialConstant.ZZ_CREDENTIAL);
        params.put("certificateStatus", CredentialConstant.CERTIFICATE_APTITUDE_STATUS_FIVE);
        List<BaseCredential> baseCredentials = baseCredentialDao.findAllCredentialByNo(params);
        if (StringUtils.isNotEmpty(baseCredentials)) {
            //新增判断
            if ("add".equals(params.get("saveType"))) {
                if (baseCredentials.size() > 0) {
                    ValidateUtils.isTrue(false, CertificateExcption.class, "证书编号重复");
                }
            } else if ("modify".equals(params.get("saveType"))) {
                Boolean bool = true;
                for (BaseCredential baseCredential : baseCredentials) {
                    if (Long.parseLong(params.get("qualificationId").toString()) != baseCredential.getId()) {
                        bool = false;
                    }
                }
                ValidateUtils.isTrue(bool, CertificateExcption.class, "证书编号重复");
            }
        }
    }


    /**
     * 地区处理
     *
     * @param areaName
     * @param areaCode 选中的地区代码
     */
    public Map<String, String> dealArea(String areaName, String areaCode) {
        Map<String, String> araes = new HashMap<>();
        ValidateUtils.isTrue(StringUtils.isNotEmpty(areaName), CertificateExcption.class, "地区信息为空！");
        String[] name = areaName.split("/");
        String[] names = areaName.split(",");
        if (name.length > 1 || names.length > 1) { // 选择了省和市
            Map treeBookObj = cmsService.getTreeBookObj(areaCode);
            return treeBookObj;
        }
        return null;
    }

    /**
     * 根据生产订单id查询证书（证书新增时绑定的生产订单）
     *
     * @param id
     * @return
     */
    public List<BaseCredential> findCertificateByProductId(Long id) {
        return baseCredentialDao.queryGertificateByAddScOrderId(id);
    }
}
